package com.shuwang.goods.controller;

import com.shuwang.goods.domain.GoodsInfoRequest;
import com.shuwang.goods.exception.BadRequestExitException;
import com.shuwang.goods.exception.ConflictException;
import com.shuwang.goods.exception.ForbiddenException;
import com.shuwang.goods.exception.NotFoundException;
import com.shuwang.goods.mapper.GoodsCollectionMapper;
import com.shuwang.goods.mapper.GoodsInfoMapper;
import com.shuwang.goods.model.GoodsCollection;
import com.shuwang.goods.model.GoodsCollectionExample;
import com.shuwang.goods.model.GoodsInfo;
import com.shuwang.goods.model.GoodsInfoExample;
import com.shuwang.goods.until.DTBuilder;
import com.shuwang.goods.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/5.
 */
@RestController
@RequestMapping(value = "goods")
@Api(value = "goodsInfo",description = "商品基本信息")
public class GoodsInfoController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @RequestMapping(value = "goods/info", method = RequestMethod.POST)
    @ApiOperation(value = "录入商品基本信息")
    public List<GoodsInfo> createdGoodsInfo(@ApiParam(value = "商品基本信息实体类") @RequestBody GoodsInfoRequest request) {

        GoodsInfoExample goodsInfoExample =new GoodsInfoExample();
        goodsInfoExample.createCriteria().andTitleEqualTo(request.getTitle());
        if(!goodsInfoMapper.selectByExample(goodsInfoExample).isEmpty()){
            log.debug("该商品基本信息已存在");
            throw new ConflictException("该商品基本信息已存在");
        }

        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setCreateTime(new Date());
        Long goodsId = IDBuilder.CreateAnyId("110");
        goodsInfo.setGoodsId(goodsId);
        BeanUtils.copyProperties(request,goodsInfo);
        goodsInfoMapper.insertSelective(goodsInfo);

        List<GoodsInfo> success = goodsInfoMapper.selectByExample(goodsInfoExample);
        if(success == null){
            log.debug("商品基本信息入库失败");
            throw new NotFoundException("商品基本信息入库失败");
        }
        return success;
    }

    @RequestMapping(value = "info/{goodsId}", method = {RequestMethod.POST,RequestMethod.PUT})
    @ApiOperation(value = "根据产品ID修改产品基础信息")
    public GoodsInfo modifygoods(
            @ApiParam(value = "产品基本信息实体类") @PathVariable Long	 goodsId,
            @ApiParam(value = "产品基本信息实体类") @Valid @RequestBody GoodsInfoRequest request) {

        if (goodsInfoMapper.selectByPrimaryKey(goodsId)==null) {
            log.debug("该产品ID不存在，请重新确认信息。");
            throw new NotFoundException("该产品ID不存在，请重新确认信息。");
        }
        GoodsInfo goodsInfo= new GoodsInfo();
        BeanUtils.copyProperties(request,goodsInfo);
        goodsInfo.setGoodsId(goodsId);

        goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);
        if(goodsInfo == null ){
            log.debug("修改产品入库信息失败");
            throw new NotFoundException("修改产品入库信息失败");
        }
        return goodsInfoMapper.selectByPrimaryKey(goodsId);
    }

    @RequestMapping(value = "goods/getInfo/{goodsId}",method = {RequestMethod.GET})
    @ApiOperation(value = "根据goodsId查询商品基本信息", httpMethod = "", response = GoodsInfo.class)
    public List<GoodsInfo> getGoodsInfoBygoodsIdController(@ApiParam(value = "请输入goodsId") @PathVariable Long goodsId ) {

        GoodsInfoExample goodsInfoExample =new GoodsInfoExample();
        goodsInfoExample.createCriteria().andGoodsIdEqualTo(goodsId);
        List<GoodsInfo> goodsInfo =goodsInfoMapper.selectByExample(goodsInfoExample);

        if( goodsInfo.isEmpty()){
            log.debug("该商品基本信息不存在");
            throw new NotFoundException("该商品基本信息不存在");
        }
        return goodsInfo;
    }

    @RequestMapping(value = "info/title/{title}}", method = RequestMethod.GET)
    @ApiOperation(value = "根据产品标题查询产品基础信息")
    public List<GoodsInfo> getInfoByTitle(@ApiParam(value = "产品标题") @PathVariable String title) {
        GoodsInfoExample example=new GoodsInfoExample();
        example.createCriteria().andTitleEqualTo(title);
        if (example==null)
        {
            log.debug("该产品ID不存在，请重新确认信息。");
            throw new NotFoundException("该产品ID不存在，请重新确认信息。");
        }
        List<GoodsInfo> success=goodsInfoMapper.selectByExample(example);
        return success;
    }

    @RequestMapping(value = "info/time/{time}}", method = RequestMethod.GET)
    @ApiOperation(value = "根据时间查询产品基础信息")
    public List<GoodsInfo> getInfoByTime(
            @ApiParam(value = "开始时间") @RequestParam(value = "startTime", required = false)  Long startTime,
            @ApiParam(value = "结束时间") @RequestParam(value = "endTime", required = false)  Long endTime
    ) {Date sTime = null, eTime = null;
        try {
            sTime = DTBuilder.TimeStampToDatetime(startTime);
            eTime = DTBuilder.TimeStampToDatetime(endTime);
        } catch (ParseException e) {
            log.debug("时间出错：" + e.getMessage());
            throw new BadRequestExitException("时间出错：" + e.getMessage());
        } finally {
            if (endTime==null){
                GoodsInfoExample starttimeexample = new GoodsInfoExample();
                starttimeexample.createCriteria().andCreateTimeEqualTo(sTime);
                if (starttimeexample == null) {
                    log.debug("Sorry,未查到任何此时间内的物品，请重新确认信息。");
                    throw new NotFoundException("Sorry,未查到任何此时间内的物品，请重新确认信息。");
                }List<GoodsInfo> success = goodsInfoMapper.selectByExample(starttimeexample);
                return success;
            }
            GoodsInfoExample example = new GoodsInfoExample();
            example.createCriteria().andCreateTimeBetween(sTime, eTime);
            if (example == null) {
                log.debug("Sorry,未查到任何此时间段内的物品，请重新确认信息。");
                throw new NotFoundException("Sorry,未查到任何此时间段内的物品，请重新确认信息。");
            }
            List<GoodsInfo> success = goodsInfoMapper.selectByExample(example);
            if (success == null) {
                log.debug("Sorry,未查到任何此时间段内的物品，请重新确认信息。");
                throw new NotFoundException("Sorry,未查到任何此时间段内的物品，请重新确认信息。");
            }
            return success;
        }
    }


    @RequestMapping(value = "goods/deletegoodsInfo/{goodsId}",method = {RequestMethod.DELETE})
    @ApiOperation(value = "根据goodsId删除商品收藏信息", httpMethod = "DELETE", response = GoodsCollection.class)
    public List<GoodsInfo>  deleteGoodsInfoByGoodsIdController(@ApiParam(value = "goodsId") @PathVariable Long goodsId ) {

        GoodsInfoExample goodsInfoExample =new GoodsInfoExample();
        goodsInfoExample.createCriteria().andGoodsIdEqualTo(goodsId);
        List<GoodsInfo> goodsInfo =goodsInfoMapper.selectByExample(goodsInfoExample);
        if (goodsInfo.isEmpty()) {
            log.debug("该商品基本信息不存在");
            throw new NotFoundException("该商品基本信息不存在");
        }
        goodsInfoMapper.deleteByExample(goodsInfoExample);

        if (!goodsInfoMapper.selectByExample(goodsInfoExample).isEmpty()) {
            log.debug("该商品基本信息删除失败");
            throw new ForbiddenException("该商品基本信息删除失败");

        }
        return goodsInfo;
    }
}
